package com.jay.compass;

import com.jay.compass.app.StarApplication;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

public class LoginActivity extends Activity {

	private Button loginButton;
	private Button offlineLoginButton;
	public static final int CONNECT_TYPE_BLUETOOTH = 0;
	public static final int CONNECT_TYPE_WIFI = 1;
	public static final int CONNECT_TYPE_NONE = 2;
	public static final String CONNECT_TYPE_STRING = "CONNECT TYPE";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);

		int loginType = StarApplication.getInstance().getSpUtil()
				.getLoginType();

		if (loginType >= 0) {
			Intent intent = new Intent(LoginActivity.this, MainActivity.class);
			intent.putExtra(CONNECT_TYPE_STRING, loginType);
			startActivity(intent);
			finish();
		}

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		resourceInit();

	}

	private void resourceInit() {
		loginButton = (Button) findViewById(R.id.login_button);
		offlineLoginButton = (Button) findViewById(R.id.offlinelogin_button);
		loginButton.setOnClickListener(new ButtonListener());
		offlineLoginButton.setOnClickListener(new ButtonListener());
	}

	class ButtonListener implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (v.getId() == loginButton.getId()) {
				// TODO Auto-generated method stub

				// LoginActivity.this.finish();
			} else if (v.getId() == offlineLoginButton.getId()) {
				showSelectLoginDialog();
			}
		}
	}

	private void showSelectLoginDialog() {
		new AlertDialog.Builder(LoginActivity.this)
				.setTitle(R.string.offline_login_title)
				.setItems(R.array.connect_mode_items_dialog,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								// if (which == CONNECT_TYPE_BLUETOOTH) {
								Intent intent = new Intent(LoginActivity.this,
										MainActivity.class);
								intent.putExtra(CONNECT_TYPE_STRING, which);
								startActivity(intent);
								finish();
							}
						}).show();
	}
}
